package ro.xblue.translator;

import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: Translator.java,v 1.1 2004/10/24 23:37:02 mihaipostelnicu Exp $
 * @deprecated
 */
public class Translator
{
      private File wdir = null;
      private Hashtable h_modules;

      public Translator( File dir )
      {
            if ( ( dir == null ) || ( !dir.exists() ) || dir.isFile() )
                  throw new RuntimeException( "Incearca cu un director valid" );

            this.wdir = dir;
            h_modules = new Hashtable();

            reloadModules();
      }

      /**
       * Incarca modulul specificat
       * Daca nu a mai fost incarcat, incearca
       * il construieste
       *
       * @param name
       */
      public void reloadModule( String name )
      {
            Module module = ( Module ) h_modules.get( name );
            if ( module != null )
            {
                  module.reload();
                  return;
            }

            module = new Module( new File( wdir, name ) );
            h_modules.put( name, module );
            make_module_list();
      }

      /**
       * Reincarca modulele
       */
      public void reloadModules()
      {
            h_modules.clear();

            File[] files = wdir.listFiles();
            if ( ( files == null ) || ( files.length < 1 ) )
                  return;

            for ( int i = 0; i < files.length; i++ )
            {
                  Module module = new Module( files[ i ] );
                  h_modules.put( files[ i ].getName(), module );
            }

            make_module_list();
      }

      public String getMessage( String lang, String module_name, String label )
      {
            Module mod = ( Module ) h_modules.get( module_name );
            if ( mod == null ) return null;

            return mod.getMessage( lang, label );
      }

      public String[] getModuleNames()
      {
            return module_list;
      }

      private String[] module_list;

      private void make_module_list()
      {
            module_list = new String[ h_modules.size() ];
            Enumeration en = h_modules.keys();
            int i = 0;
            while ( en.hasMoreElements() )
            {
                  module_list[ i++ ] = ( ( Module ) h_modules.get( en.nextElement() ) ).getName();
            }
      }

      public Module getModule( String name )
      {
            if ( name == null ) return null;
            return ( Module ) h_modules.get( name );
      }

//      public static void main( String[] a )
//      {
//            Translator x = new Translator( new File( "F:\\projects\\gateway\\tomcat\\WEB-INF\\translator" ) );
//            System.out.println( x.getMessage( "ro", "users", "form.pass" ) );
//      }
}
